package com.quick.controller;

import com.quick.domain.User;
import com.quick.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jojo.wang
 * @title: LoginController
 * @projectName spring-boot-quick
 * @description: TODO
 * @date 2019/5/611:50
 */

@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("{name}/{pwd}")
    public String login(@PathVariable String name,@PathVariable String pwd){
        User user = null;
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(name, pwd);
        try {
            subject.login(token);
            user =  (User) subject.getPrincipal();
            if (user==null){
                throw new AuthenticationException();
            }
        }catch (UnknownAccountException  e) {
            log.error("用户帐号或密码不正确");
            return "用户帐号或密码不正确";
        } catch (IncorrectCredentialsException e) {
            log.error("用户名或密码错误");
            return "用户帐号或密码不正确";
        }catch (LockedAccountException e){
            return "用户帐号被锁定";
        }catch (AuthenticationException e) {
            //其他错误，比如锁定，如果想单独处理请单独catch处理
            log.error("其他错误");
            return "其他错误";
        }
        return user.toString();
    }
}
